pipeline {
    agent {
        label 'slave_agent'
    }
    stages {
        stage('拉取代码') {
            steps {
                checkout([
                    $class: 'GitSCM', 
                    branches: [
                        [name: '*/developer_params']
                    ], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [], 
                    submoduleCfg: [],
                    userRemoteConfigs: [
                        [credentialsId: 'd5415a5e-ab83-401e-98d7-c4c8c945985c', url: 'http://10.12.3.141/ATAP/frontend.git']
                    ]
                ])
            }
        }
        stage('安装依赖') {
            steps {
                sh 'npm install'
            }
        }
        stage('构建打包') {
            steps {
                sh 'npm run build'
            }
        }
        stage('准备文件') {
            steps {
                sh 'rm -rf ./docker/atap-frontend'
                sh 'rm -rf ./docker/persistence/html/'
                sh 'mkdir ./docker/persistence/html/'
                sh 'cp -r dist/* ./docker/persistence/html/'
                sh 'rm -f /home/atapserver/atap-frontend/docker-compose.yml'
                sh 'rm -f /home/atapserver/atap-frontend/persistence/start.sh'
                sh 'rm -rf /home/atapserver/atap-frontend/persistence/conf'
                sh 'rm -rf /home/atapserver/atap-frontend/persistence/html'
                sh 'cp -r ./docker/* /home/atapserver/atap-frontend/'


                script {
                    try {
                        def mm = readFile("/home/atapserver/atap-frontend/persistence/logs")
                    } catch (exc) {
                        sh 'mkdir -p /home/atapserver/atap-frontend/persistence/logs'
                    }
                    try {
                        def mm = readFile("/home/atapserver/atap-frontend/persistence/cache")
                    } catch (exc) {
                        sh 'mkdir -p /home/atapserver/atap-frontend/persistence/cache'
                    }
                }
            }
        }
        stage('停止镜像') {
            steps {
                sh 'docker-compose -f /home/atapserver/atap-frontend/docker-compose.yml down --rmi all'
            }
        }
        stage('启动容器') {
            steps {
                sh 'docker-compose -f /home/atapserver/atap-frontend/docker-compose.yml up -d'
            }
        }
    }
}
